package tjs.study.mini.mybatismini.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import tjs.study.mini.mybatismini.config.annotion.MiniMapper;

/**
 * 调用注册器后置处理器，执行自定义扫描逻辑
 * 对标以下类
 *
 * @author nicky
 * @date 2021/9/1 下午5:38
 * @see org.mybatis.spring.mapper.MapperScannerConfigurer
 */
public class MapperScannerConfigurerRegistrar implements BeanDefinitionRegistryPostProcessor {

    private String[] basePackages;

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        //扫描类
        ClassPathMapperScanner scanner = new ClassPathMapperScanner(beanDefinitionRegistry, false);

        //只扫描@MiniMapper
        scanner.addIncludeFilter(new AnnotationTypeFilter(MiniMapper.class));
        scanner.scan(basePackages);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }

    public String[] getBasePackages() {
        return basePackages;
    }

    public void setBasePackages(String[] basePackages) {
        this.basePackages = basePackages;
    }
}
